package dynamic_handle;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.Clock;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import DB.DB_connection_pool;


public class leaving_msg_to_attention extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter out = response.getWriter();
		String receive_leaving_messages = request.getParameter("receive_user_account");
		String leaving_content = request.getParameter("leaving_content");
		String send_leaving_messages = request.getSession().getAttribute("user_account").toString();
		Clock clock = Clock.systemUTC();
		String leaving_id = String.valueOf((int)(Math.random()*900000)+100000)+clock.millis();  //评论编号;
		java.sql.Date leaving_date = new java.sql.Date(clock.millis());     //当前时间
		
		String insert_leaving = "insert into leaving_messages(send_leaving_messages,receive_leaving_messages,leaving_content,leaving_date,leaving_id) values(?,?,?,?,?)";
		JSONObject jsonObj = new JSONObject();
		try {
			Connection conn = DB_connection_pool.data_pool.getConnection();
			PreparedStatement ps_insert = conn.prepareStatement(insert_leaving);
			ps_insert.setString(1, send_leaving_messages);
			ps_insert.setString(2, receive_leaving_messages);
			ps_insert.setString(3, leaving_content);
			ps_insert.setDate(4, leaving_date);
			ps_insert.setString(5, leaving_id);
			ps_insert.executeUpdate();
			
			
			jsonObj.put("result", true);
		} catch (SQLException e) {
			jsonObj.put("result", false);
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		out.print(jsonObj.toString());
	}

}
